package com.example.demo.controller.usercontroller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.ErrorCode;
import com.example.demo.common.Result;
import com.example.demo.context.UserContext;
import com.example.demo.dto.ChildUpdateDTO;
import com.example.demo.entity.Child;
import com.example.demo.service.ChildService;
import com.example.demo.vo.ChildProfileVO;
import com.example.demo.vo.GrowthRecordVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Child)表控制层
 *
 * @author makejava
 * @since 2025-01-27 22:47:00
 */
@RestController
@RequestMapping("user/child")
@Tag(name = "客户端孩子接口")
@RequiredArgsConstructor
@Slf4j
public class ChildController {
    private final ChildService childService;

    @GetMapping("/list")
    @Operation(description = "获取孩子列表")
    public Result<List<Child>> list() {
        return Result.success(childService.listAll());
    }

    @GetMapping("/profile/{childId}")
    public Result<ChildProfileVO> getChildProfile(@PathVariable Long childId) {
        return Result.success(childService.getChildProfile(childId));
    }

    @PostMapping("/update")
    public Result updateChild(@RequestBody ChildUpdateDTO childUpdateDTO) {
        // 1. 参数校验
        if (childUpdateDTO.getId() == null) {
            return Result.error(ErrorCode.SYSTEM_ERROR);
        }
        if (StringUtils.isBlank(childUpdateDTO.getName())) {
            return Result.error(ErrorCode.SYSTEM_ERROR);
        }
        if (StringUtils.isBlank(childUpdateDTO.getGender())) {
            return Result.error(ErrorCode.SYSTEM_ERROR);
        }
        if (childUpdateDTO.getBirthday() == null) {
            return Result.error(ErrorCode.SYSTEM_ERROR);
        }

        try {
            // 2. 检查儿童是否存在
            Child existingChild = childService.getById(childUpdateDTO.getId());
            if (existingChild == null) {
                return Result.error(ErrorCode.SYSTEM_ERROR);
            }

            // 3. 检查权限（确保当前用户是这个儿童的父母/监护人）
            Long currentUserId = UserContext.getUserId();
            if (!existingChild.getUserId().equals(currentUserId)) {
                return Result.error(ErrorCode.SYSTEM_ERROR);
            }

            // 4. 更新儿童信息
            Child child = new Child();
            BeanUtils.copyProperties(childUpdateDTO, child);

         /*   // 5. 如果有新头像，删除旧头像（可选）
            if (StringUtils.isNotBlank(childUpdateDTO.getAvatar())
                    && StringUtils.isNotBlank(existingChild.getAvatar())
                    && !childUpdateDTO.getAvatar().equals(existingChild.getAvatar())) {
                // 从OSS删除旧头像
                ossService.deleteFile(existingChild.getAvatar());
            }*/

            // 6. 保存更新
            childService.updateById(child);

            return Result.success("更新成功");
        } catch (Exception e) {
            log.error("更新儿童信息失败", e);
            return Result.error(ErrorCode.SYSTEM_ERROR);
        }
    }


    @PostMapping("add")
    @Operation(description = "添加孩子")
    public Result add(@RequestBody Child child) {
        child.setUserId(UserContext.getUserId());
        childService.save(child);
        return Result.success();
    }


}

